package cnpm02.j2ee.group3.logic.validator;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.validator.Validator;
import org.jboss.seam.Component;

public class UniqueValidator implements Validator<Unique> {
	private String sql;
	private String param;
	public void initialize(Unique value) {
		sql = value.sql();
		param = value.parameterName();
	}

	public boolean isValid(Object value) {
		EntityManager entityManager = (EntityManager) Component
				.getInstance("entityManager");
		Query query = entityManager.createQuery(sql);
		query.setParameter(param, value);
		if (query.getResultList().size() > 0) {
			return false;
		} else {
			return true;
		}
	}

}
